package com.example.mybatis.utils;

public class DataSourceHolder
{
    public static String MASTER_DB = "master";
    public static String SLAVE_DB = "slave";
    static ThreadLocal<String> dataSourceMap = new ThreadLocal<>();

    public static void setDataSource(String dataSourceKey){
        if(MASTER_DB.equals(dataSourceKey) || SLAVE_DB.equals(dataSourceKey))
            dataSourceMap.set(dataSourceKey);
        else
            dataSourceMap.set(MASTER_DB);
    }

    public static String getDataSource(){
        return dataSourceMap.get() == null ? MASTER_DB : dataSourceMap.get() ;
    }

    public static void remove(){
        dataSourceMap.remove();
    }
}
